package com.samoiloff.framework {
	import com.samoiloff.framework.controller.AbstractController;
	import com.samoiloff.framework.model.AbstractModel;
	import com.samoiloff.framework.view.AbstractView;

	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author Samoiloff (http://blog.samoiloff.com)
	 */
	public class AbstractComponent extends Sprite {
		
		protected var _model		: AbstractModel;
		protected var _view			: AbstractView;
		protected var _controller	: AbstractController;

		public function AbstractComponent() {

			var modelCls:Class = getModelClass();
			_model = new modelCls() as AbstractModel;

			var viewCls:Class = getViewCls();
			_view = new viewCls(this, _model) as AbstractView;

			var controllerCls:Class = getControllerCls();
			_controller = new controllerCls(_view);
		}

		public function get ready():Boolean {
			return _model.ready;
		}

		public function destroy():void {
			if (_controller) {
				_controller.destroy();
				_controller = null;
			}

			if (_view) {
				_view.destroy();
				_view = null;
			}
			
			if (_model) {
				_model.destroy();
				_model = null;
			}
		}
		
		protected function getModelClass():Class {
			return AbstractModel;
		}
		
		protected function getViewCls():Class {
			return AbstractView;
		}
		
		protected function getControllerCls():Class {
			return AbstractController;
		}
		
	}
}
